package com.core;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.util.Assert;

import com.puman.common.BeanUtils;
import com.puman.common.GenericsUtils;
import com.puman.common.MyException;

public class BaseServiceImpl<T, M extends BaseDao<T>> implements BaseService<T> {
	private M baseDao;
	protected Pager pager=new Pager();
	@SuppressWarnings("unchecked")
	protected M getBaseDao() throws MyException {
		if (baseDao != null) {
			return baseDao;
		}
		List<Field> fields = BeanUtils.getFieldsByType(this,
				GenericsUtils.getSuperClassGenricType(getClass(), 1));
		try {
			baseDao = (M) BeanUtils.forceGetProperty(this, fields.get(0)
					.getName());
		} catch (NoSuchFieldException e) {
			e.printStackTrace();
			throw new MyException("Dao未能成功初始化", e);
		}
		Assert.notNull(baseDao, "Dao未能成功初始化");
		return baseDao;
	}

	public T get(Integer id) throws MyException {
		try {
			return getBaseDao().get(id);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}

	public T load(Integer id) throws MyException {
		try {
			return getBaseDao().load(id);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}

	public List<T> get(Integer[] ids) throws MyException {
		try {
			return getBaseDao().get(ids);
		} catch (Exception e) {
			e.printStackTrace();
			return new ArrayList<T>();
		}

	}

	public T get(String propertyName, Object value) throws MyException {
		try {
			return getBaseDao().get(propertyName, value);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public List<T> getList(String propertyName, Object value)
			throws MyException {
		try {
			return getBaseDao().getList(propertyName, value);
		} catch (Exception e) {
			e.printStackTrace();
			return new ArrayList<T>();
		}
	}

	public List<T> getAll() throws MyException {
		try {
			return getBaseDao().getAll();
		} catch (Exception e) {
			e.printStackTrace();
			return new ArrayList<T>();
		}
	}

	public Long getTotalCount() throws MyException {
		try {
			return getBaseDao().getTotalCount();
		} catch (Exception e) {
			e.printStackTrace();
			return Long.valueOf(0);
		}
	}

	public boolean isUnique(String propertyName, Object oldValue,
			Object newValue) throws MyException {
		try {
			return getBaseDao().isUnique(propertyName, oldValue, newValue);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	public boolean isExist(String propertyName, Object value)
			throws MyException {
		try {
			return getBaseDao().isExist(propertyName, value);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	public Integer save(T entity) throws MyException {
		try {
			return getBaseDao().save(entity);
		} catch (Exception e) {
			e.printStackTrace();
			return Integer.valueOf(0);
		}
	}
	public String add(T entity) throws MyException {
		try {
			return getBaseDao().add(entity);
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}
	public void update(T entity) throws MyException {
		try {
			getBaseDao().update(entity);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void delete(T entity) throws MyException {
		try {
			getBaseDao().delete(entity);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void delete(Integer id) throws MyException {
		try {
			getBaseDao().delete(id);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void delete(Integer[] ids) {
		try {
			getBaseDao().delete(ids);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void flush() {
		getBaseDao().flush();
	}

	public void clear() {
		getBaseDao().clear();
	}

	public void evict(Object object) {
		getBaseDao().evict(object);
	}

	public Pager findByPager(Pager pager) throws MyException {
		try {
			return getBaseDao().findByPager(pager);
		} catch (Exception e) {
			e.printStackTrace();
			return new Pager();
		}
	}

	public Pager findByPager(Pager pager, DetachedCriteria detachedCriteria)
			throws MyException {
		try {
			return getBaseDao().findByPager(pager, detachedCriteria);
		} catch (Exception e) {
			e.printStackTrace();
			return new Pager();
		}
	}

	@Override
	public List<T> getListLike(Map<String, Object> propertyMap) {
		try {
			return getBaseDao().getListLike(propertyMap);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}

	@Override
	public List<T> getList(String hql) {
		try {
			return getBaseDao().getList(hql);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	@Override
	public Integer getTotalCountLike(Map<String, Object> propertyMap)
			throws MyException {
		try {
			Integer totalCount=0;
			Long temp=getBaseDao().getTotalCountLike(propertyMap);
			if(temp!=null){
				totalCount=Integer.parseInt(temp+"");
			}
				
			return totalCount;
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}

	}

	@Override
	public Pager findByPagerLike(Pager pager, Map<String, Object> property) {
		try {
			return getBaseDao().findByPagerLike(pager, property);
		} catch (Exception e) {
			e.printStackTrace();
			return new Pager();
		}
	}

	@Override
	public Pager findByPagerEquel(Pager pager, Map<String, Object> property) {
		try {
			return getBaseDao().findByPagerEquel(pager, property);
		} catch (Exception e) {
			e.printStackTrace();
			return new Pager();
		}
	}

	@Override
	public Pager findByPagerEquel(Pager pager, Map<String, Object> property,
			String operation, String date) {
		try {
			return getBaseDao().findByPagerEquel(pager, property,operation,date);
		} catch (Exception e) {
			e.printStackTrace();
			return new Pager();
		}
	}

	@Override
	public Pager findByPagerEquel(Pager pager, Map<String, Object> property,
			String operation1, String date1, String operation2, String date2) {
		try {
			return getBaseDao().findByPagerEquel(pager, property,operation1,date1,operation2,date2);
		} catch (Exception e) {
			e.printStackTrace();
			return new Pager();
		}
	}
	public Pager findByPager(Pager pager, JSONObject params) {
		List<T> list = new ArrayList<T>();
		HashMap<String, Object> map = new HashMap<String, Object>();

		if (params != null && !params.isEmpty()) {
			params = JSONObject.fromObject(params.toString()
					.replaceAll("\\[", "").replaceAll("\\]", ""));
			Iterator it = params.keys();
			while (it.hasNext()) {
				String key = (String) it.next();
				String value = params.getString(key);
				if (StringUtils.isNotBlank(value)) {
					map.put(key, value);
				}
			}
		}
		if (map.size() > 0) {
			pager = getBaseDao().findByPagerLike(pager, map);
		} else {
			pager = getBaseDao().findByPager(pager);
		}
		return pager;
	}
	public Pager findByPager(Pager pager, JSONObject params,JSONObject params2) {
		List<T> list = new ArrayList<T>();
		HashMap<String, Object> map = new HashMap<String, Object>();
		HashMap<String, Object> map2 = new HashMap<String, Object>();
		if (params != null && !params.isEmpty()) {
			params = JSONObject.fromObject(params.toString()
					.replaceAll("\\[", "").replaceAll("\\]", ""));
			Iterator it = params.keys();
			while (it.hasNext()) {
				String key = (String) it.next();
				String value = params.getString(key);
				if (StringUtils.isNotBlank(value)) {
					map.put(key, value);
				}
			}
		}
		if (params2 != null && !params2.isEmpty()) {
			params2 = JSONObject.fromObject(params2.toString()
					.replaceAll("\\[", "").replaceAll("\\]", ""));
			Iterator it = params2.keys();
			while (it.hasNext()) {
				String key = (String) it.next();
				String value = params2.getString(key);
				if (StringUtils.isNotBlank(value)) {
					map2.put(key, value);
				}
			}
		}
		if (map.size() > 0 || map2.size() > 0) {
			pager = getBaseDao().findByPagerLike(pager, map,map2);
		} else {
			pager = getBaseDao().findByPager(pager);
		}
		return pager;
	}
}
